﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Win32Wrapper
{
    public class IconExtractor
    {
        public static System.Windows.Media.ImageSource ExtractIconFromFile(String FileName)
        {
            ushort dummy;
            StringBuilder file = new StringBuilder(FileName);
            IntPtr handle = Win32.Win32Helper.ExtractAssociatedIcon(IntPtr.Zero, file, out dummy);

            return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(handle, System.Windows.Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
        }

        public static System.Windows.Media.ImageSource ExtractIconFromWindow(IntPtr hWnd)
        {
            //IntPtr handle = Win32.Win32Helper.ExtractAssociatedIcon(IntPtr.Zero, file, out dummy);

            IntPtr handle = (IntPtr)Win32.Win32Helper.SendMessage(hWnd, 0x007F, 2, 0);

            return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(handle, System.Windows.Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
        }
    }
}
